package com.zhugang.week13.array;

/**
 * @program algorithms
 * @description: reverseString
 * @author: chanzhugang
 * @create: 2022/09/17 22:43
 */
public class ReverseString {

    /**
     * 344 反转字符串
     * https://leetcode.cn/problems/reverse-string/
     *
     * @param s
     */
    public void reverseString(char[] s) {
        // 数组上的特殊排序，想到双指针
        int i = 0;
        int j = s.length - 1;
        while (i <= j) {
            char temp = s[i];
            s[i] = s[j];
            s[j] = temp;
            i++;
            j--;
        }
    }


    public void reverseString2(char[] s) {
        // 遍历到一半位置结束
        int n = s.length;
        for (int i = 0; i < s.length / 2; i++) {
            char temp = s[i];
            s[i] = s[n - i - 1];
            s[n - i - 1] = temp;
        }
    }
}